# -*- coding: utf8 -*-
#import sys
#reload(sys)
#sys.setdefaultencoding('utf-8')
import DNS, re
import csv
import xlrd


def ReadFile(Filename):
    suffix= Filename.split(".")
    if suffix[-1]== "xlsx":
        data = xlrd.open_workbook(Filename)
        table = data.sheet_by_index(0)
        nrows = table.nrows
        domainList={}
        for i in range(nrows):
            domainList[table.row_values(i)[1]]= table.row_values(i)[3]
    elif suffix[-1]=="txt":
        data= open(Filename,'r')
        domainList = {}
        for i in data.readlines():
            domainList[i.strip()]="网信分析"
    return domainList

def FindCNAME(domain):
    DNS.DiscoverNameServers()
    reqobj = DNS.Request()
    answerobj = reqobj.req(domain)
    x = answerobj.answers
    for donequeries in x:
        if donequeries['typename']=="CNAME": #判断是否含有CNAME
            answers = []
            re_ip = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') #判断IP
            for i in x:
                if not re_ip.match(i['data']):#直到IP停止
                    answers=i['data']
            return answers #返回最后一个CNAME
        else:
            return "none"

def ProcessData(cname):
    types={
            "蓝讯": ["ccgslb.net", "ccgslb.com", "ccgslb.com.cn", "chinacache.net", "chinacache.com.cn", "lxsvc.cn"],
        "网宿": ["glb0.lxdns.com", "ourdvs.com", "ourwebpic.com", "ourwebcdn.com", "ourglb0.com", "ourwebhttps.com",
               "ourwebat.com"],
        "快网": ["fastweb.com", "fastwebcdn.com", "cloudcdn.net", "cloudglb.com", "cachecn.com", "cachecn.net",
               "hadns.net", "hacdn.net"],
        "帝联": ["dnion.com", "ewcache.com", "globalcdn.cn", "tlgslb.com", "fastcdn.com"],
        "百度": ["jomodns.com"],
        "京东": ["jdcdn.com"],
                "淘宝":["alikunlun.com"
                        ,"alikunlun.net"
                        ,"kunlunaq.com"
                        ,"kunlunar.com"
                        ,"kunlunca.com"
                        ,"kunluncan.com"
                        ,"kunlunea.com"
                        ,"kunlungem.com"
                        ,"kunlungr.com"
                        ,"kunlunhuf.com"
                        ,"kunlunle.com"
                        ,"kunlunli.com"
                        ,"kunlunno.com"
                        ,"kunlunpi.com"
                        ,"kunlunra.com"
                        ,"kunlunsa.com"
                        ,"kunlunsc.com"
                        ,"kunlunsl.com"
                        ,"kunlunso.com"
                        ,"kunlunta.com"
                        ,"kunlunvi.com"
                        ,"kunlunwe.com"
                        ,"alicdn.com"
                        ,"tbcache.com"
                        ],
        "新浪": ["sinaedge.com"],
        "爱奇艺": ["dns.iqiyi.com"],
        "PPTV": ["cloudxns.pptv.com"],
        "腾讯":["tc.qq.com","tcdn.qq.com","myqcloud.com","dayugslb.com","tencent-cloud.net","tcdnvod.com","tcdnlive.com","cdntip.com"],
        "沙塔": ["satacdn.com", "sihuacdn.com.cn", "okeycdn.com"],
        "云帆": ["yflive.net", "yunfancdn.com"],
        "金山云": ["ks-cdn.com", "ksyuncdn.com"],
        "白山云": ["qingcdn.com", "bsgslb.com", "trpcdn.net", "bsgslb.cn", "bsclink.cn"],
        "迅雷星域": ["p2cdn.com", "00cdn.com", "xycdn.com"],
        "又拍云": ["b0.upaiyun.com", "b0.aicdn.com"],
        "小米": ["mgslb.com"],
        "乐视云": ["gslb.lecloud.com", "lsyun.net", "cdnle.com"],
		}
    all={}
    for t in types.iteritems():
        for hz in t[1]:
            all[hz]=t[0]
        for type in all.iteritems():
            if len(cname.split(type[0]))==2:
                return type[1]
    return "CNAME不在服务列表中"





if __name__=="__main__":
    #读取文件返回字典{域名：公司名}
    domainList=ReadFile("dns.txt")
    data=[]
    with open('dnslog.csv', 'wb') as csvfile:
        spamwriter = csv.writer(csvfile,delimiter=' ',
                            quotechar=',', quoting=csv.QUOTE_MINIMAL)
        for domain in domainList.iteritems():
            answers = FindCNAME(domain[0])
            if not answers=="none"and answers:
                CP_NAME=ProcessData(answers)
            else:
                CP_NAME="无CNAME"
            spamwriter.writerow([domain[0],domain[1],CP_NAME])
            #WrlteFile(domain[0], domain[1], CP_NAME)
        #print domain#每行最后新加覆盖CP标识
